<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataCompany extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $db = Db::name('data_company'); 
        if(!empty($param['kw'])){
            $db = $db->where('title','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        return view('',['list'=>$list,'count'=>$count,'param'=>$param]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = Db::name('data_company')->insert($param);
            if( $result ) {
                xn_add_admin_log('添加企业');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = Db::name('data_company')->update($param);
            if( $result ) {
                xn_add_admin_log('修改企业');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        $data = Db::name('data_company')->find($id);
        return view('form',['data'=>$data]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_company')->delete($id);
        xn_add_admin_log('删除企业');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        Db::name('data_company')->delete($ids);
        xn_add_admin_log('批量删除企业');
        $this->success("删除成功");
    }

    public function import(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::putFileAs( 'excel', $file, 'enterprise.xlsx');
        if(!$savename){
            return json(['status'=>'0','msg'=>'文件上传失败!']);
        }
        $filePath = \think\facade\Filesystem::path($savename);

        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)) {
                return json(['status'=>'0','msg'=>'No Excel!']);
            }
        }
        
        $PHPExcel = $PHPReader->load($filePath);
        $currentSheet = $PHPExcel->getSheet(0);
        //$allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();
        
        $limitNum = 10;
        $limitPage = ceil($allRow / $limitNum);
        $count = 0;
        for($i=0;$i<$limitPage;$i++){
            $limitIndex = $i * $limitNum;
            $limitEnd = $limitIndex + $limitNum;
            if($i == 0){
                $limitIndex = 1;
            }
            if($limitEnd >= $allRow){
                $limitEnd = $allRow+1;
            }
            for($currentRow = $limitIndex; $currentRow < $limitEnd; $currentRow++) {
                $title = $currentSheet->getCell('A'.$currentRow)->getValue();
                $r = Db::name('data_company')->where([
                    'title' => $title
                ])->find();
                if(!$r){
                    $com = apiGetCom($title);
                    $com = json_decode($com,true);
                    if(!empty($com['result'])){
                        $com = $com['result'];
                        $com['title'] = $title;
                        $insertId = Db::name('data_company')->insertGetId($com);
                        if($insertId){
                            $count++;
                        }
                    }
                }
            }
        }
        return json(['status'=>'1','msg'=>'成功导入 '.$count.' 条数据']);
    }
    

}
